package com.wenzhiguo.myheadline.fragment.voide;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wenzhiguo.myheadline.Bean.VideoBean;
import com.wenzhiguo.myheadline.R;
import com.wenzhiguo.myheadline.view.XListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;


public class New_Voide_Fragment extends Fragment implements XListView.IXListViewListener {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private View view;
    private XListView xlv;
    private ArrayList<VideoBean> al;
    int page = 10;
    private DisplayImageOptions options;

    public New_Voide_Fragment() {
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .build();
    }

    public static New_Voide_Fragment newInstance(String param1, String param2) {
        New_Voide_Fragment fragment = new New_Voide_Fragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.new_voide_fragment, container, false);
        //控件
        initView();
        //请求数据
        Request(mParam2, page);

        return view;
    }
    //控件
    private void initView() {
        al=new ArrayList<>();
        xlv = (XListView) view.findViewById(R.id.xlv_voide);
        xlv.setPullRefreshEnable(true);
        xlv.setPullLoadEnable(true);
        xlv.setXListViewListener(this);
    }

    private void Request(final String a, int b) {
        String url = RequestUrl(a, b);
        //请求
        x.http().get(new RequestParams(url), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                //解析
                Map<String, List<VideoBean>> map = new Gson().fromJson(result, new TypeToken<Map<String, List<VideoBean>>>() {
                }.getType());
                Log.d("zzz", "onSuccess: "+result);
                List<VideoBean> list = map.get(a);
                Log.d("zzz", "onSuccess: " + list);
                //添加到list
                al.addAll(list);
                Log.d("zzz", "onSuccess: "+al.toString());
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }
            //适配器
            @Override
            public void onFinished() {
                MyVoideBaseAdapter adapter = new MyVoideBaseAdapter();
                //适配
                if (page == 10) {
                    xlv.setAdapter(adapter);

                } else {
                    adapter.notifyDataSetChanged();
                }

            }
        });
    }
    //请求路径
    private String RequestUrl(String c, int d) {
        String url = String.format("http://c.3g.163.com/nc/video/list/%1$s/n/%2$s-10.html", c, d);
        return url;
    }
    //刷新
    @Override
    public void onRefresh() {
        page = 10;
        al.clear();
        Request(mParam2, 10);
        //停止刷新和加载
        xlv.stopRefresh();
        xlv.stopLoadMore();
        //时间
        getDate();
    }
    //加载更多
    @Override
    public void onLoadMore() {
        page += 10;
        Request(mParam2, page);
        //停止刷新和加载
        xlv.stopRefresh();
        xlv.stopLoadMore();
        getDate();
    }

    private void getDate() {
        //时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd  hh-mm-ss");
        String str = format.format(date);
        xlv.setRefreshTime(str);
    }

    class MyVoideBaseAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return al.size();
        }

        @Override
        public Object getItem(int position) {
            return al.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView=View.inflate(getActivity(),R.layout.video_list,null);
            JCVideoPlayerStandard jc=(JCVideoPlayerStandard)convertView.findViewById(R.id.cao);
            ImageView image=(ImageView)convertView.findViewById(R.id.voide_image);
            TextView title=(TextView)convertView.findViewById(R.id.voide_title);

            jc.setUp(al.get(position).getMp4_url(), al.get(position).getTitle());

            ImageLoader.getInstance().displayImage(al.get(position).getTopicImg(),image,options);
            ImageLoader.getInstance().displayImage(al.get(position).getCover(),jc.thumbImageView,options);
            title.setText(al.get(position).getTopicName());
            return convertView;

        }
    }
}
